package org.myspringmvc.web.servlet.view;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.Data;
import org.myspringmvc.web.servlet.View;

import java.util.Map;

/**
 * @author 莫磊
 * @date 2025/8/12
 * @description 内部资源视图，解析jsp的
 */
@Data
public class InternalResourceView implements View {

    private String contentType = "text/html;charset=UTF-8";

    private String path;

    @Override
    public String getContentType() {
        return contentType;
    }

    @Override
    public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception {

        // 渲染
        // 设置响应内容类型
        response.setContentType(contentType);
        // 将model数据存储到request作用域中
        model.forEach(request::setAttribute);
        // 转发，默认情况下，跳转到视图是以转发的方式进行
        request.getRequestDispatcher(path).forward(request, response);
    }
}
